package com.mixsmart.web.main;

import java.util.ArrayList;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.mixsmart.commonweb.IWebConstant;
import com.mixsmart.commonweb.bean.entity.TCDict;
import com.mixsmart.commonweb.bean.entity.TCMenu;
import com.mixsmart.commonweb.bean.entity.TCRole;
import com.mixsmart.commonweb.bean.entity.TCRoleMenu;
import com.mixsmart.commonweb.bean.entity.TCRoleUser;
import com.mixsmart.commonweb.bean.entity.TCUrlResource;
import com.mixsmart.commonweb.bean.entity.TCUser;
import com.mixsmart.commonweb.enums.OpenModelType;
import com.mixsmart.commonweb.enums.UrlResourceType;
import com.mixsmart.commonweb.service.impl.ServiceContext;
import com.mixsmart.enums.YesNoType;
import com.mixsmart.security.SecurityUtils;

/**
 * 初始化系统数据
 * @author lmq
 * @version 1.0
 * @since JDK版本大于等于1.6
 * 2016年2月1日
 */
public class InitSystemData {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		@SuppressWarnings("resource")
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		ServiceContext servContext = context.getBean(ServiceContext.class);
		try {
			new InitSystemData().initData(servContext);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	public void initData(ServiceContext servContext) {
		TCRole role = new TCRole("超级管理员角色", "超级管理员角色", YesNoType.YES.getIndex(), IWebConstant.ROLE_SUPER_ADMIN);
		servContext.getRoleServ().save(role);
		String md5Password = SecurityUtils.md5("123456");
		TCUser user = new TCUser("admin", md5Password, "管理员", YesNoType.YES.getIndex(), "0");
		servContext.getDictServ().saveObj(user);
		TCRoleUser roleUser = new TCRoleUser(role.getId(), user.getId());
		servContext.getUserServ().saveObj(roleUser);
			
		TCMenu parentMenu = new TCMenu("系统管理", IWebConstant.TREE_ROOT_ID, "",  0, YesNoType.YES.getIndex(),"", 0, false);
		servContext.getMenuServ().save(parentMenu);
		TCRoleMenu roleMenu = new TCRoleMenu(role.getId(), parentMenu.getId());
		servContext.getMenuServ().saveObj(roleMenu);
			
		TCUrlResource menuUrl = new TCUrlResource("菜单管理", "menu/list.do", YesNoType.YES.getIndex(), 
					UrlResourceType.MENU_URL_RESOURCE.getValue());
		servContext.getUrlResServ().save(menuUrl);
			
		TCUrlResource resUrl = new TCUrlResource("资源管理", "showPage/web_urlResource_list.do", YesNoType.YES.getIndex(), 
					UrlResourceType.MENU_URL_RESOURCE.getValue());
		servContext.getUrlResServ().save(resUrl);
			
		TCMenu menu = new TCMenu("菜单管理", parentMenu.getId(), OpenModelType.IFRAME.getValue(), 1, YesNoType.YES.getIndex(), "", menuUrl.getId(), 1, true);
		servContext.getMenuServ().save(menu);
		TCMenu resMenu = new TCMenu("资源管理", parentMenu.getId(), OpenModelType.IFRAME.getValue(), 2, YesNoType.YES.getIndex(), "", resUrl.getId(), 1, true);
		servContext.getMenuServ().save(resMenu);
		TCRoleMenu roleMenu2 = new TCRoleMenu(role.getId(), menu.getId());
		servContext.getMenuServ().saveObj(roleMenu2);
		TCRoleMenu roleMenu3 = new TCRoleMenu(role.getId(), resMenu.getId());
		servContext.getMenuServ().saveObj(roleMenu3);
			
		//初始化数据字典
		ArrayList<TCDict> dicts = new ArrayList<TCDict>();
		TCDict parentDict = new TCDict("状态", "valid_state", IWebConstant.TREE_ROOT_ID, YesNoType.YES.getIndex(), 1, 0, false);
		servContext.getDictServ().save(parentDict);
		TCDict dict = new TCDict("有效","1",parentDict.getId(), YesNoType.YES.getIndex(), 1, 1, true);
		dicts.add(dict);
		dict = null;
		dict = new TCDict("无效","0",parentDict.getId(), YesNoType.YES.getIndex(), 2, 1, true);
		dicts.add(dict);
			
		TCDict resTypeParentDict = new TCDict("资源类型", "url_resource_type", IWebConstant.TREE_ROOT_ID, YesNoType.YES.getIndex(), 2, 0, false);
		servContext.getDictServ().save(resTypeParentDict);
		dict = new TCDict("菜单资源",UrlResourceType.MENU_URL_RESOURCE.getValue(),resTypeParentDict.getId(), YesNoType.YES.getIndex(), 1, 1, true); 
		dicts.add(dict);
		dict = new TCDict("页面资源",UrlResourceType.PAGE_URL_RESOURCE.getValue(),resTypeParentDict.getId(), YesNoType.YES.getIndex(), 2, 1, true);
		dicts.add(dict);
		dict = new TCDict("流程资源",UrlResourceType.FLOW_URL_RESOURCE.getValue(),resTypeParentDict.getId(), YesNoType.YES.getIndex(), 3, 1, true);
		dicts.add(dict);

		TCDict openModelParentDict = new TCDict("菜单打开模式", "menu_open_model", IWebConstant.TREE_ROOT_ID, YesNoType.YES.getIndex(), 3, 0, false);
		servContext.getDictServ().save(openModelParentDict);
		dict = new TCDict("iframe模式",OpenModelType.IFRAME.getValue(),openModelParentDict.getId(), YesNoType.YES.getIndex(), 1, 1, true); 
		dicts.add(dict);
		dict = new TCDict("直接载入模式",OpenModelType.LOCATION.getValue(),openModelParentDict.getId(), YesNoType.YES.getIndex(), 2, 1, true);
		dicts.add(dict);
		dict = new TCDict("打开新页面",OpenModelType.BLANK.getValue(),openModelParentDict.getId(), YesNoType.YES.getIndex(), 3, 1, true);
		dicts.add(dict);
		dict = new TCDict("弹出窗口模式",OpenModelType.DIALOG.getValue(),openModelParentDict.getId(), YesNoType.YES.getIndex(), 3, 1, true);
		dicts.add(dict);
		servContext.getDictServ().save(dicts);
			
		/*******是否状态*****/
		dicts = new ArrayList<TCDict>();
		TCDict parentDict3 = new TCDict("是否状态", "yes_or_no", IWebConstant.TREE_ROOT_ID, YesNoType.YES.getIndex(), 1, 0, false);
		servContext.getDictServ().save(parentDict3);
		TCDict dict31 = new TCDict("是", "1", parentDict3.getId(), YesNoType.YES.getIndex(), 1, 1, true);
		dicts.add(dict31);
		dict31 = null;
		dict31 = new TCDict("否", "0", parentDict3.getId(), YesNoType.YES.getIndex(), 1, 1, true);
		dicts.add(dict31);
		servContext.getDictServ().save(dicts);
			
		System.out.println("系统初始化数据[成功]");
	}
}
